package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallTopic;
import com.cskaoyan.bean.CskaoyanmallTopicExample;
import com.cskaoyan.bean.vo.WxTopicDetailVoZGY;
import com.cskaoyan.bean.vo.WxTopicListVoZGY;
import com.cskaoyan.mapper.CskaoyanmallTopicMapper;
import com.cskaoyan.service.WxTopicServiceZGY;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WxTopicServiceImplZGY implements WxTopicServiceZGY {

    @Autowired
    CskaoyanmallTopicMapper mapper;

    /**
     * 查询所有的topic
     */
    @Override
    public WxTopicListVoZGY queryTopics(Integer page, Integer size) {
        PageHelper.startPage(page, size);

        CskaoyanmallTopicExample topicExample = new CskaoyanmallTopicExample();
        List<CskaoyanmallTopic> topics = mapper.selectByExample(topicExample);

        // TODO: 去除掉，多余的部分。都置为null

        PageInfo<CskaoyanmallTopic> topicPageInfo = new PageInfo<>(topics);
        long total = topicPageInfo.getTotal();

        WxTopicListVoZGY wxTopicListVoZGY = new WxTopicListVoZGY();
        wxTopicListVoZGY.setCount(total);
        wxTopicListVoZGY.setData(topics);


        return wxTopicListVoZGY;
    }

    @Override
    public WxTopicDetailVoZGY queryTopicDetail(Integer id) {

        WxTopicDetailVoZGY detailVo = new WxTopicDetailVoZGY();

        CskaoyanmallTopic topic = mapper.selectByPrimaryKey(id);
//        detailVo.setGoods(topic.getGoods());

        detailVo.setTopic(topic);

        return detailVo;
    }

    @Override
    public List<CskaoyanmallTopic> QueryRelatedTopics(Integer id) {

        // 先查出goodsid再根据goodsid查相关的topic
        CskaoyanmallTopic topic = mapper.selectByPrimaryKey(id);
        Integer[] goods = topic.getGoods();

        List<CskaoyanmallTopic> topics = new ArrayList<>();
        for (int i = 0; i < goods.length; i++) {

            CskaoyanmallTopic topicRelated = mapper.selectByPrimaryKey(goods[i]);
            topics.add(topicRelated);

        }

        return topics;
    }
}
